package com.dede.tablayouttest

import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.RippleDrawable
import android.os.Build
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val colors by lazy {
        arrayListOf(0xFF109D59, 0xFF109D59/*0xFFFF4081*/, 0xFFFF1643, 0xFFFF5622, 0xFF039BE5, 0xFF9D27B0)
    }

    private val titles by lazy {
        arrayListOf("首页", "游戏", "影视", "音乐", "图书", "报刊")
//        arrayListOf("维保查询", "车型判定", "精准定价", "保卖定价", "车史定价")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.statusBarColor = Color.TRANSPARENT
        }

        setSupportActionBar(tool_bar)

        val viewPagerAdapter = ViewPagerAdapter(titles)
        view_pager.adapter = viewPagerAdapter
        tab_layout.setupWithViewPager(view_pager)
        val tabGroup = tab_layout.getChildAt(0) as ViewGroup

        tab_layout.tabMode = TabLayout.MODE_FIXED// fixed模式下测量宽度

        tab_layout.measure(0, 0)
        val tabLayoutWidth = tab_layout.measuredWidth
        val screenWidth = resources.displayMetrics.widthPixels
        if (tabLayoutWidth > screenWidth) {
            Toast.makeText(this, "TabLayoutWidth:$tabLayoutWidth, TabMode:MODE_SCROLLABLE",
                    Toast.LENGTH_SHORT).show()
            tab_layout.tabMode = TabLayout.MODE_SCROLLABLE
        } else {
            Toast.makeText(this, "TabLayoutWidth:$tabLayoutWidth, TabMode:MODE_FIXED",
                    Toast.LENGTH_SHORT).show()
            tab_layout.tabMode = TabLayout.MODE_FIXED
        }
        view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                val tab = tabGroup.getChildAt(position)
                val color = colors[position].toInt()
                color_view.startAnimWithView(color, tab)
            }
        })
    }

    class ViewPagerAdapter(private val titles: ArrayList<String>) : PagerAdapter() {

        override fun isViewFromObject(view: View?, `object`: Any?) = view == `object`

        override fun getCount() = titles.size

        override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
            container?.removeView(`object` as View?)
        }

        override fun getPageTitle(position: Int): CharSequence {
            return titles[position]
        }

        override fun instantiateItem(container: ViewGroup?, position: Int): Any {
            val imageView = ImageView(container!!.context)
            val rippleDrawable = RippleDrawable(
                    ColorStateList.valueOf(Color.GRAY),
                    ColorDrawable(Color.WHITE),
                    null)
            imageView.background = rippleDrawable
            imageView.isClickable = true
            container.addView(imageView, ViewPager.LayoutParams())
            return imageView
        }
    }


}
